﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.WebPage;

namespace ZHDJ.Web
{
    public partial class RedirectTo : PortalPage
    {
        public string BackUrl
        {
            get
            {
                var temp= (Request.QueryString["ReturnUrl"]??"") == ""
                  ? "/Default.aspx"
                  : Server.UrlDecode(Request.QueryString["ReturnUrl"]);
                if (temp.Contains("?"))
                {
                    temp += "&"+OtherPara;
                }
                else
                {
                    temp += "?" + OtherPara;
                }
                return temp;
            }
        }

        public string ToType
        {
            get
            {
                return (Request.QueryString["ToType"]??"") == ""
                  ? "default"
                  : Request.QueryString["ToType"];
            }
        }

        public string OtherPara
        {
            get
            {
                var query= Request.Url.Query;
                if (string.IsNullOrEmpty(query))
                {
                    return "";
                }
                var paras = query.TrimStart('?').Split('&').ToList();
                var temp = paras.Where(a => a.Contains("ReturnUrl=") || a.Contains("ToType=")).ToList();
                temp.ForEach(a => { paras.Remove(a); });
                return string.Join("&",paras.ToArray());
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Clear();
            //Response.Write("<script></script>");
            //Response.End();
            //return;
            //Response.Redirect(GetUrl());
        }

        public string GetUrl()
        {
            var url = "/Default.aspx";
            switch (ToType)
            {
                case "Login":
                    url = "/login.aspx?ReturnUrl=" + Server.UrlEncode(BackUrl);
                    break;
                case "MassMain":
                    url = "/app/MassMain.aspx";
                    break;
                case "PersonalMain":
                    url = "/app/PersonalMain.aspx";
                    break;
                case "LoginSu":
                    url = BackUrl;
                    break;
            }
            
            return url.ToLower().IndexOf("http://") > 0 ? url : WebHost + url;

        }
    }
}